package com.ruoyi.deeplearn.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.deeplearn.domain.DeepDataset;
import com.ruoyi.deeplearn.domain.DeepDatasetGroup;
import com.ruoyi.deeplearn.service.IDeepDatasetGroupService;
import com.ruoyi.deeplearn.service.IDeepDatasetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据集组Controller
 *
 * @author rww
 * @date 2022-12-10
 */
@RestController
@RequestMapping("/deeplearn/datasetgroup")
public class DeepDatasetGroupController extends BaseController
{
    @Autowired
    private IDeepDatasetGroupService deepDatasetGroupService;
    @Autowired
    private IDeepDatasetService deepDatasetService;

    /**
     * 查询数据集组列表
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:datasetgroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeepDatasetGroup deepDatasetGroup)
    {
        startPage();
        List<DeepDatasetGroup> list = deepDatasetGroupService.
                selectDeepDatasetGroupList(deepDatasetGroup);
        if(list!=null){
            list.stream().forEach(item->{
                DeepDataset query = new DeepDataset();
                query.setDatasetGroup(item.getId());
                List<DeepDataset> deepDatasets = deepDatasetService.selectDeepDatasetList(query);
                item.setDatasets(deepDatasets);
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出数据集组列表
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:datasetgroup:export')")
    @Log(title = "数据集组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeepDatasetGroup deepDatasetGroup)
    {
        List<DeepDatasetGroup> list = deepDatasetGroupService.selectDeepDatasetGroupList(deepDatasetGroup);
        ExcelUtil<DeepDatasetGroup> util = new ExcelUtil<DeepDatasetGroup>(DeepDatasetGroup.class);
        util.exportExcel(response, list, "数据集组数据");
    }

    /**
     * 获取数据集组详细信息
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:datasetgroup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deepDatasetGroupService.selectDeepDatasetGroupById(id));
    }

    /**
     * 新增数据集组
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:datasetgroup:add')")
    @Log(title = "数据集组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeepDatasetGroup deepDatasetGroup)
    {
        int result = deepDatasetGroupService.createDatasetGroup(deepDatasetGroup);
        return toAjax(result);
    }

    /**
     * 修改数据集组
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:datasetgroup:edit')")
    @Log(title = "数据集组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeepDatasetGroup deepDatasetGroup)
    {
        return toAjax(deepDatasetGroupService.updateDeepDatasetGroup(deepDatasetGroup));
    }

    /**
     * 删除数据集组
     */
//    @PreAuthorize("@ss.hasPermi('deeplearn:datasetgroup:remove')")
    @Log(title = "数据集组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deepDatasetGroupService.deleteDeepDatasetGroupByIds(ids));
    }
}
